(function($){

    $.fn.extend({
        serializeObject: function () {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function () {
                if (o[this.name] !== undefined) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        }
    });

    //首先备份下jquery的ajax方法
    var _ajax=$.ajax;

    //重写jquery的ajax方法
    $.ajax=function(opt){
        //备份opt中error和success方法
        var fn = {
            error:function(XMLHttpRequest, textStatus, errorThrown){},
            success:function(data, textStatus){}
        }
        if(opt.error){
            fn.error=opt.error;
        }
        if(opt.success){
            fn.success=opt.success;
        }

        //扩展增强处理
        var _opt = $.extend(opt,{
            error:function(XMLHttpRequest, textStatus, errorThrown){
                //错误方法增强处理
                fn.error(XMLHttpRequest, textStatus, errorThrown);
            },
            success:function(data, textStatus){
                //成功回调方法增强处理
                fn.success(data, textStatus);
            },
            beforeSend:function(XHR){
                // $('body').append("<div id='ajaxInfo' style='width: 100%;height: 100%'>正在加载,请稍后...</div>");
            },
            complete:function(XHR, TS){
                //请求完成后回调函数 (请求成功或失败之后均调用)。
                // $("#ajaxInfo").remove();
            }
        });
        return _ajax(_opt);
    };

    $.extend({
        /**
         * 成功提示
         * */
        optAjax: function (opt) {
            //备份opt中error和success方法
            var fn = {
                success: function (data, textStatus) {
                }
            };
            if (opt.success) {
                fn.success = opt.success;
            }

            //扩展增强处理
            var _opt = $.extend(opt, {
                success: function (data, textStatus) {
                    $("button").button("reset");
                    if (data.type!='success'){
                        swal({
                            title: data.title,
                            text: data.text,
                            type: data.type,
                            closeOnConfirm: false
                        },function () {
                            swal.close();
                        });
                        // 如果执行是错误的操作,就终止下一步操作
                        // return;
                    }else {
                        swal({
                            title: data.title,
                            text: data.text,
                            type: data.type,
                            closeOnConfirm: false
                        },function () {
                            swal.close();
                        });
                    }
                    fn.success(data, textStatus);
                }
            });
            return $.ajax(_opt);
        },

        /***
         *  成功提示和重新加载页面
         * */
        reloadAjax: function (opt) {
            //备份opt中error和success方法
            var fn = {
                success: function (data, textStatus) {
                }
            };
            if (opt.success) {
                fn.success = opt.success;
            };

            //扩展增强处理
            var _opt = $.extend(opt, {
                success: function (data, textStatus) {
                    $("button").button("reset");
                    if (data.type!='success'){
                        swal({
                            title: data.title,
                            text: data.text,
                            type: data.type,
                            closeOnConfirm: false
                        },function () {
                            swal.close();
                        });
                        // return;
                        // 如果执行是错误的操作,就终止下一步操作
                    }else {
                        swal({
                            title: data.title,
                            text: data.text,
                            type: data.type,
                            closeOnConfirm: false
                        },function () {
                            swal.close();
                            location.reload();
                        });
                    }
                    fn.success(data, textStatus);
                }
            });
            return $.ajax(_opt);
        },

        /***
         *  成功不做任何操作
         * */
        defaultAjax: function (opt) {
            //备份opt中error和success方法
            var fn = {
                success: function (data, textStatus) {
                }
            };
            if (opt.success) {
                fn.success = opt.success;
            };

            //扩展增强处理
            var _opt = $.extend(opt, {
                success: function (data, textStatus) {
                    $("button").button("reset");
                    if (data.type!='success'){
                        swal({
                            title: data.title,
                            text: data.text,
                            type: data.type,
                            closeOnConfirm: false
                        },function () {
                            swal.close();
                        });
                        // return;
                        // 如果执行是错误的操作,就终止下一步操作
                    }
                    fn.success(data, textStatus);
                }
            });
            return $.ajax(_opt);
        },

        /**
         *
         * */
        urlUtil:{
            replace : function (msg) {
                window.location.replace(msg);

            },
            href : function (msg) {
                window.location.href = msg;
            },
        },

        /***
        *   时间戳转换工具
        * **/
        DateUtil: {
            format: function (now, mask) {
                var d = now;
                if (typeof d !== 'object') {
                    d = new Date();
                    d.setTime(now);
                }

                var zeroize = function (value, length) {
                    if (!length) length = 2;
                    value = String(value);
                    for (var i = 0, zeros = ''; i < (length - value.length); i++) {
                        zeros += '0';
                    }
                    return zeros + value;
                };
                if (mask === undefined) mask = 'yyyy-MM-dd HH:mm:ss';
                return mask.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])\1?|[lLZ])\b/g, function ($0) {
                    switch ($0) {
                        case 'd':
                            return d.getDate();
                        case 'dd':
                            return zeroize(d.getDate());
                        case 'ddd':
                            return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][d.getDay()];
                        case 'dddd':
                            return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][d.getDay()];
                        case 'M':
                            return d.getMonth() + 1;
                        case 'MM':
                            return zeroize(d.getMonth() + 1);
                        case 'MMM':
                            return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][d.getMonth()];
                        case 'MMMM':
                            return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][d.getMonth()];
                        case 'yy':
                            return String(d.getFullYear()).substr(2);
                        case 'yyyy':
                            return d.getFullYear();
                        case 'h':
                            return d.getHours() % 12 || 12;
                        case 'hh':
                            return zeroize(d.getHours() % 12 || 12);
                        case 'H':
                            return d.getHours();
                        case 'HH':
                            return zeroize(d.getHours());
                        case 'm':
                            return d.getMinutes();
                        case 'mm':
                            return zeroize(d.getMinutes());
                        case 's':
                            return d.getSeconds();
                        case 'ss':
                            return zeroize(d.getSeconds());
                        case 'l':
                            return zeroize(d.getMilliseconds(), 3);
                        case 'L':
                            var m = d.getMilliseconds();
                            if (m > 99) m = Math.round(m / 10);
                            return zeroize(m);
                        case 'tt':
                            return d.getHours() < 12 ? 'am' : 'pm';
                        case 'TT':
                            return d.getHours() < 12 ? 'AM' : 'PM';
                        case 'Z':
                            return d.toUTCString().match(/[A-Z]+$/);
                        // Return quoted strings with the surrounding quotes removed
                        default:
                            return $0.substr(1, $0.length - 2);
                    }
                });
            }
        }
    });






})(jQuery);